package Dao;

import DataClass.Student;
import Util.JBDCUtil;
import javafx.util.Pair;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDao {
    private static PreparedStatement getPreparedStatement = null;
    private static PreparedStatement updatePreparedStatement = null;

    public static Student getStudent(String accountId) throws SQLException {
        Student user = new Student();
        ResultSet data;
        try {
            data = StudentDao.getStudentData(accountId);
        } catch (SQLException e) {
            System.out.println("学生个人信息查询失败");
            throw new SQLException();
        }

        while (data.next()) {
            String SCname = data.getString("SCname");
            String SType = data.getString("SType");
            Integer SEnter = data.getInt("SEnter");
            String STNo = data.getString("STNo");
            String SATNo = data.getString("SATNo");
            String SRemark = data.getString("SRemark");
            java.sql.Timestamp SIn = data.getTimestamp("SIn");
            java.sql.Timestamp SOut = data.getTimestamp("SOut");
            String SPassword = data.getString("SPassword");
            user = new Student(accountId,SCname,SType,SEnter,STNo,SATNo,SRemark,SIn,SOut,SPassword);
        }

        // 释放资源
//        DatabaseUtil.closeResultSet(data);
        return user;
    }

    /**
     * 获取学生用户的信息
     * @return 返回学生信息
     * @throws SQLException 数据库错误
     */
    public static ResultSet getStudentData(String SNo) throws SQLException{
        if(getPreparedStatement == null)
        {
            String sql = "select * from Student WHERE sno = ?;";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getPreparedStatement = JBDCUtil.getPreparedStatement(sql);
        }
        getPreparedStatement.setString(1, SNo);
        ResultSet resultSet = getPreparedStatement.executeQuery();
        JBDCUtil.commit();

        return resultSet;
    }

    /**
     * 学生更新个人密码
     * @param SNo 学生学号
     * @param SPassword 学生密码
     * @throws SQLException 数据库异常
     */
    public static void updatePassword(String SNo, String SPassword) throws SQLException{
        if(updatePreparedStatement == null)
        {
            String sql = "UPDATE Student SET " +
                "SPassword = ? " +
                "WHERE sno = ?;" +
                "UPDATE Registered_users SET " +
                "RU_password = ?" +
                "WHERE RU_no = ?;";
            updatePreparedStatement = JBDCUtil.getPreparedStatement(sql);
        }

        // 设置参数，注意索引从1开始
        updatePreparedStatement.setString(1, SPassword);
        updatePreparedStatement.setString(2, SNo);    // 最后设置WHERE子句中的条件
        updatePreparedStatement.setString(3, SPassword);
        updatePreparedStatement.setString(4, SNo);    // 最后设置WHERE子句中的条件

        // 执行更新操作
        int rowsAffected = updatePreparedStatement.executeUpdate();

        // 处理更新结果
        if (rowsAffected > 0) {
            System.out.println("更新成功。影响的行数：" + rowsAffected);
        } else {
            System.out.println("没有进行更新操作或没有行被影响。");
        }
    }

    /**
     * 获取某一个用户的两个导师
     * @param Account 账户id
     * @return 键值对 键为主教师 值为副教师
     */
    public static Pair<String,String> getTeachers(String Account){
        Pair<String,String> result;

        try{
            ResultSet data = StudentDao.getStudentData(Account);

            data.next();
            String STNo = data.getString("STNo");
            String SATNo = data.getString("SATNo");

            result = new Pair<>(STNo, SATNo);
            JBDCUtil.closeResultSet(data);
            return result;
        }catch(SQLException e){
            System.err.println("查找导师失败 from StudentDao");
        }
        return null;
    }
}
